package com.bomberdroid.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.bomberdroid.controller.GameBombeButton;
import com.bomberdroid.controller.GameControls;
import com.bomberdroid.controller.GameJoystick;
import com.bomberdroid.model.BuildMap;
import com.bomberdroid.model.Map;

/**
 * 
 * @author Jerome This class displays ll the elements of the game
 * 
 */

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {

	private static final String TAG = GamePanel.class.getSimpleName();

	public static boolean server;

	private MainThread thread;
	private GameControls controls;
	private Map map;
	private int ind = 0;
	private int width;
	private int height;
	private Context context;

	public GamePanel(Activity context, int mapId, boolean serv) {
		super(context);
		this.context = context;
		GamePanel.server = serv;

		// adding the callback (this) to the surface holder to intercept events
		SurfaceHolder holder = getHolder();
		getHolder().addCallback(this);

		width = context.getWindowManager().getDefaultDisplay().getWidth();
		height = context.getWindowManager().getDefaultDisplay().getHeight();

		// create the game loop thread
		thread = new MainThread(holder, context, new Handler(),this);
		

		map = BuildMap.Lire(mapId, getContext().getResources(), width, height);

		controls = new GameControls();

		controls.joystick = new GameJoystick(getContext().getResources(), map.getPlayer(ind), width, height);

		controls.bombeButton = new GameBombeButton(getContext().getResources(), map.getPlayer(ind), width, height);

		setOnTouchListener(controls);

		// make the GamePanel focusable so it can handle events
		/**
		 * inutile
		 */
		setFocusable(true);

	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	}

	public void surfaceCreated(SurfaceHolder holder) {

		if(thread.state==MainThread.PAUSED){
			//When game is opened again in the Android OS
			thread = new MainThread(getHolder(), context, new Handler(),this);
			thread.state = MainThread.RUNNING;
			thread.start();
		}else{
			//creating the game Thread for the first time
			thread.state = MainThread.RUNNING;
			thread.start();
		}
		// at this point the surface is created and
		// we can safely start the game loop
		//thread.setRunning(true);
		//thread.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "Surface is being destroyed");
		
		thread.state = MainThread.STOPPED;

		// tell the thread to shut down and wait for it to finish
		// this is a clean shutdown
		boolean retry = true;
		
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}

		// ((Activity)this.getContext()).finish();

		Log.d(TAG, "Thread was shut down cleanly");
	}

	/**
	 * Displayer
	 */
	@Override
	protected void onDraw(Canvas canvas) {

		// Paint paint = new Paint();

		canvas.drawColor(Color.BLACK);

		int posX = map.getPlayer(ind).getX();
		int posY = map.getPlayer(ind).getY();

		int posXmax = map.getMapWidth();
		int posYmax = map.getMapHeight();

		if (posX > width / 2) {
			if (posX < posXmax - width / 2)
				canvas.translate(width / 2 - posX, 0);
			else
				canvas.translate(width - posXmax, 0);
		}

		if (posY > height / 2) {
			if (posY < posYmax - height / 2)
				canvas.translate(0, height / 2 - posY);
			else
				canvas.translate(0, height - posYmax);
		}

		map.onDraw(canvas);

		// draw the joystick

		canvas.restore();
		controls.joystick.onDraw(canvas);
		controls.bombeButton.onDraw(canvas);

		canvas.save();
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public int getInd() {
		return ind;
	}

	public void setInd(int ind) {
		this.ind = ind;
		this.controls.joystick.setPlayer(this.map.getPlayer(ind));
		this.controls.joystick.setIndicePlayer(ind);
		this.controls.bombeButton.setPlayer(this.map.getPlayer(ind));
		this.controls.bombeButton.setIndicePlayer(ind);
	}

	public GameControls getControls() {
		return controls;
	}

	public void setControls(GameControls controls) {
		this.controls = controls;
	}

	public MainThread getThread() {
		return thread;
	}
}
